package com.cn.kabalifeuser.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.event.RxBus;
import com.cn.kabalifeuser.event.RxBusBean;
import com.cn.kabalifeuser.utils.RxToast;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by 晓林 on 2020/8/13.
 * function:
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, "wx5419c854322ca75c");

        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                if (Constants.PAY_SIGN == 2001) {
                    //商品购买成功2001
                    // OrderConfirmActivity
                    RxBus.getDefault().post(new RxBusBean(2001, 2001));
                } else if (Constants.PAY_SIGN == 2002) {
                    //订单列表购买成功2002
                    //  MallOrderFragments
                    RxBus.getDefault().post(new RxBusBean(2002, 2002));
                } else if (Constants.PAY_SIGN == 2003) {
                    //订单详情购买成功2002
                    // MallOrderDetailsActivity
                    RxBus.getDefault().post(new RxBusBean(2003, 2003));
                } else if (Constants.PAY_SIGN == 2004) {
                    //服务订单列表的待支付
                    // ServiceOrderFragments
                    RxBus.getDefault().post(new RxBusBean(2004, 2004));
                } else if (Constants.PAY_SIGN == 2005) {
                    //服务待僱傭金的支付
                    //DepositPayActivity
                    RxBus.getDefault().post(new RxBusBean(2005, 2005));
                }


                finish();
            } else if (resp.errCode == -1) {
                // 支付异常
                RxToast.error("支付异常");
//                if (Constants.PAY_SIGN == 2001) {
//                    RxBus.getDefault().post(new RxBusBean(20011, 20011));
//                }
//                finish();
            } else if (resp.errCode == -2) {
                // 支付取消
                RxToast.error("取消支付");
//                if (Constants.PAY_SIGN == 2001) {
//                    RxBus.getDefault().post(new RxBusBean(20011, 20011));
//                }
//                finish();
            }
            finish();
        }
    }
}
